clear all;

load('input.mat','Z');

cd ..; cd ..; cd ..;
load('code_main_model\6_implied_w_r\computation_results\data.mat','frac_credit','pdf_B_forward','cash','deposit','d');
cd 'code_figure_table\OA\Figure OA7';

ZZ=unique(Z);
mkt=find(Z==ZZ(3));

cash=cash(1,mkt);
deposit=deposit(1,mkt);
dc_ratio=deposit./cash;

frac_credit=frac_credit(1,mkt);
mktsize=squeeze(sum(pdf_B_forward(1,mkt,:,:,:),[3,4,5]));
frac_credit=frac_credit./mktsize;

d=d(1,mkt);

[~,id]=sort(d);

d=d(id);
dc_ratio=dc_ratio(id);
frac_credit=frac_credit(id);

index=find(frac_credit==0,1,'first');
d_credit=d(1:index);
frac_credit=frac_credit(1:index);
index=find(dc_ratio==0,1,'first');
d_deposit=d(1:index);
dc_ratio=dc_ratio(1:index);

save('result.mat','d_credit','d_deposit','dc_ratio','frac_credit')